package com.weirong.smartvault.backservice.service;

import com.weirong.smartvault.dao.CashboxRealtimeRepository;
import com.weirong.smartvault.entity.CashboxConf;
import com.weirong.smartvault.entity.CashboxRealtime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.util.List;

@Service
public class CashboxRealtimeService {

    @Autowired
    private CashboxRealtimeRepository cashboxRealtimeRepository;

    public List<CashboxRealtime> getCashboxRealtimeByUnitNo(int unitNo)
    {
        Sort sort=new Sort(Sort.Direction.ASC,"slot_no");
        return this.cashboxRealtimeRepository.findCashboxRealtimeByUnitNo(unitNo, sort);
    }
    public Page<CashboxRealtime> findAllByUnitNoWithPaging(Integer unitNo, Integer page, Integer count) {
        Specification<CashboxRealtime> specification = (root, criteriaQuery, criteriaBuilder) -> {
            Path<Integer> No = root.get("unitNo");
            Predicate predicate = criteriaBuilder.equal(No,unitNo);
            return criteriaBuilder.and(predicate);
        };

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, count, sort);
        return cashboxRealtimeRepository.findAll(specification,pageable);
    }
}